package com.bio.action.right;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jfree.util.Log;

import com.bio.action.AbstractAction;
import com.bio.entity.right.Menu;
import com.bio.entity.right.Typemenu;
import com.bio.entity.user.Usertype;
import com.bio.service.right.MenuService;
import com.bio.service.right.TypemenuService;
import com.bio.service.user.UsertypeService;
import com.bio.utils.Environment;
import com.bio.vo.right.MenuVo;

@Namespace("/typemenu")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/right/listTypemenus.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "typemenu!list", params={"usertypeId","${usertypeId}"}, type = "redirect"),
		@Result(name = "edit", location = "/WEB-INF/views/right/editTypemenu.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/right/addTypemenu.jsp", type = "dispatcher")
		})
public class TypemenuAction extends AbstractAction<Typemenu, TypemenuService> {

	private static final long serialVersionUID = -1381254377551849920L;

	private UsertypeService usertypeService;
	
	private List<Usertype> allUsertypes;
	
	private MenuService menuService;
	
	private List<Menu> allMenus;
	
	private List<MenuVo> allMenuVos;
	
	private List<Menu> firstClassMenus;
	
	private Long companyId;
	
	private Long usertypeId;
	
	private Long superiorId;
	
	private Long[] menuIds;
	
	public TypemenuAction(TypemenuService service) {
		super(service);
	}

	public String list(){
		companyId = Environment.Session.getCompanyId();
		allUsertypes = usertypeService.findPagerByCompanyId(null, companyId).getResult();
		firstClassMenus = menuService.findMenusBySuperior(0L).getResult();
		return "list";
	}

	public void findMenuBySuperiorId(){
		try {
			allMenuVos = menuService.findMenuVosBySuperior(superiorId, usertypeId);
		} catch (Exception e) {
			Log.error(e);
		} 
		JSONArray json = JSONArray.fromObject(allMenuVos);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			System.out.println("json: " +json.toString());
			out.write(json.toString());
		} catch (IOException e) {
			Log.error(e);
		} finally {
			out.flush();
			out.close();
		}
		
	}
	
	public String update(){
		this.service.saveOrUpdateAll(menuIds, usertypeId);
		return "tolist";
	}
	
	public List<Usertype> getAllUsertypes() {
		return allUsertypes;
	}

	public void setAllUsertypes(List<Usertype> allUsertypes) {
		this.allUsertypes = allUsertypes;
	}

	public List<Menu> getAllMenus() {
		return allMenus;
	}

	public void setAllMenus(List<Menu> allMenus) {
		this.allMenus = allMenus;
	}

	public Long getCompanyId() {
		return companyId;
	}

	public void setCompanyId(Long companyId) {
		this.companyId = companyId;
	}

	public void setUsertypeService(UsertypeService usertypeService) {
		this.usertypeService = usertypeService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public List<Menu> getFirstClassMenus() {
		return firstClassMenus;
	}

	public void setFirstClassMenus(List<Menu> firstClassMenus) {
		this.firstClassMenus = firstClassMenus;
	}

	public Long getUsertypeId() {
		return usertypeId;
	}

	public void setUsertypeId(Long usertypeId) {
		this.usertypeId = usertypeId;
	}

	public Long getSuperiorId() {
		return superiorId;
	}

	public void setSuperiorId(Long superiorId) {
		this.superiorId = superiorId;
	}

	public List<MenuVo> getAllMenuVos() {
		return allMenuVos;
	}

	public void setAllMenuVos(List<MenuVo> allMenuVos) {
		this.allMenuVos = allMenuVos;
	}

	public Long[] getMenuIds() {
		return menuIds;
	}

	public void setMenuIds(Long[] menuIds) {
		this.menuIds = menuIds;
	}

}
